<template>
  <div>
    <!-- 三级分类 -->
    <Category :scene="scene"></Category>
    <el-card style="margin: 10px 0">
      <!-- 场景1:默认显示数据 -->
      <div v-show="scene === 0">
        <el-button
          type="primary"
          icon="Plus"
          :disabled="!categoryStore.c3Id"
          @click="addSpu"
          >添加SPU</el-button
        >
        <!-- 表格:展示已有的SPU数据 -->
        <el-table style="margin: 10px 0" border :data="records">
          <el-table-column
            label="序号"
            type="index"
            align="center"
            width="80px"
          ></el-table-column>
          <el-table-column label="SPU名称" prop="spuName"></el-table-column>
          <el-table-column
            label="SPU描述"
            prop="description"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column label="SPU操作">
            <template #="{ row, $index }">
              <el-button
                type="primary"
                size="small"
                icon="Plus"
                title="添加SKU"
                @click="addSku(row)"
              ></el-button>
              <el-button
                type="warning"
                size="small"
                icon="Edit"
                title="修改SPU"
                @click="updateSpu(row)"
              ></el-button>
              <el-button
                type="info"
                size="small"
                icon="View"
                title="查看SKU"
                @click="findSku(row)"
              ></el-button>
              <el-popconfirm
                :title="`你确定删除${row.spuName}`"
                @confirm="deleteSpu(row.id)"
              >
                <template #reference>
                  <el-button
                    type="danger"
                    size="small"
                    icon="Delete"
                    title="删除SPU"
                  ></el-button>
                </template>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页器 -->
        <el-pagination
          v-model:current-page="pageNo"
          v-model:page-size="pageSize"
          :page-sizes="[3, 5, 7, 9]"
          :background="true"
          layout="prev, pager, next, jumper,->,sizes,total"
          :total="total"
          @current-change="getHasSpu"
          @size-change="changeSize"
        />
      </div>
      <!-- 添加SPU|修改SPU -->
      <SpuForm
        ref="SpuFormRef"
        v-show="scene === 1"
        @changeScene="changeScene"
      ></SpuForm>
      <!-- 添加SKU -->
      <SkuForm
        ref="SkuFormRef"
        v-show="scene === 2"
        @changeScene="changeScene"
      ></SkuForm>
      <!-- dialog对话框 用于展示sku -->
      <el-dialog title="SKU列表" v-model="isShowDialog">
        <el-table border :data="skuArr">
          <el-table-column label="SKU名字" prop="skuName"></el-table-column>
          <el-table-column label="SKU价格" prop="price"></el-table-column>
          <el-table-column label="SKU重量" prop="weight"></el-table-column>
          <el-table-column label="SKU图片" align="center">
            <template #="{ row, $index }">
              <img
                :src="row.skuDefaultImg"
                alt=""
                style="width: 100px; height: 100px"
              />
            </template>
          </el-table-column>
        </el-table>
      </el-dialog>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, watch, onBeforeUnmount } from "vue";
// 引入调用接口方法
import { reqHasSpu, reqDeleteSpu, reqSkuList } from "@/api/product/spu";
import type { Records, SpuData } from "@/api/product/spu/type";
// 引入子组件
import SpuForm from "./spuForm.vue";
import SkuForm from "./skuForm.vue";
// 引入分类小仓库
import useCategoryStore from "@/store/modules/category";
import { ElMessage } from "element-plus";
const categoryStore = useCategoryStore();
const scene = ref(0); // 0:默认展示 1:添加修改spu 2:添加sku

// 分页器默认页码
const pageNo = ref<number>(1);
// 每一页默认展示几条数据
const pageSize = ref<number>(5);
// 存储已有的SPU数据
const records = ref<Records>([]);
// 存储数据总数
const total = ref<number>(0);
// 存储全部SKU数据
const skuArr = ref<any>([]);
// 控制dialog
const isShowDialog = ref(false);

// 获取子组件实例SpuFormRef
const SpuFormRef = ref<any>();
const SkuFormRef = ref<any>();
// 监听三级分类Id变化
watch(
  () => categoryStore.c3Id,
  () => {
    // 保证有三级分类id 才去发送请求
    if (!categoryStore.c3Id) {
      return;
    }
    getHasSpu();
  }
);

// 渲染页面函数
const getHasSpu = async (pager = 1) => {
  // 修改当前页码
  pageNo.value = pager;
  const res = await reqHasSpu(pageNo.value, pageSize.value, categoryStore.c3Id);
  if (res.code === 200) {
    records.value = res.data.records;
    total.value = res.data.total;
  }
};

// 分页器下拉菜单发生变化的时候触发
const changeSize = () => {
  getHasSpu();
};

// 点击添加SPU按钮的回调
const addSpu = () => {
  // 切换为场景1
  scene.value = 1;
  SpuFormRef.value.initAddSpu(categoryStore.c3Id);
};

// 子组件SpuForm绑定的自定义事件
const changeScene = (obj: any) => {
  scene.value = obj.flag;
  console.log(obj);

  if (obj.params === "update") {
    getHasSpu(pageNo.value);
  } else {
    getHasSpu();
  }
};

// 修改SPU按钮回调
const updateSpu = (row: SpuData) => {
  // 切换为场景1
  scene.value = 1;

  // 调用子组件实例的方法 获取完整的spu数据
  SpuFormRef.value.initHasSpuData(row);
};

// 删除SPU
const deleteSpu = async (id: number) => {
  const result: any = await reqDeleteSpu(id as number);
  if (result.code === 200) {
    ElMessage({
      type: "success",
      message: "删除成功",
    });
    // 获取一次已有的SPU
    getHasSpu(records.value.length > 0 ? pageNo.value : pageNo.value - 1);
  } else {
    ElMessage({
      type: "error",
      message: "删除失败",
    });
  }
};

// 添加Sku按钮回调
const addSku = (row: any) => {
  // 切换场景2
  scene.value = 2;
  console.log(row);

  SkuFormRef.value.initSkuData(categoryStore.c1Id, categoryStore.c2Id, row);
};

// 查看SKU按钮回调
const findSku = async (row: any) => {
  const res = await reqSkuList(row.id);
  if (res.code === 200) {
    skuArr.value = res.data;
    // 显示dialog
    isShowDialog.value = true;
  }
};

// 组件销毁的时候清空数据
onBeforeUnmount(() => {
  categoryStore.$reset();
});
</script>

<style scoped></style>
